/*
 * ftp_client.c
 *
 *  Created on: May 14, 2012
 *      Author: root
 */

#include "unp.h"
#include "ftp_client.h"

USER_CMD cmd_info;

int main(int argc, char **argv) {
	char user_cmd[MAXLINE];
	state = U_CMD;

	rbuf = createRbuf(MAXLINE);

	for (;;) {
		if (state == U_CMD) {

			printf("np2012 ftp>");
			fflush(stdout);

			Fgets(user_cmd, MAXLINE, stdin);

			if (!strcmp(user_cmd, "\n")) {
				memset(user_cmd, 0, strlen(user_cmd));
				continue;
			}
			cmd_info.cmd_num = sscanf(user_cmd, "%s %s %s", cmd_info.cmd,
					cmd_info.arg1, cmd_info.arg2);

			for (;;) {

				if (!strcmp(cmd_info.cmd, "open")) {
					if (connect_bit == 0) {
						cmd_info = event_open(cmd_info);
						state = I_REPLY;
						connect_bit = 1;
						break;
					} else if (connect_bit == 1) {
						printf("Already connected to %s, use close first.\n",
								first_ip);
						break;
					}
				} else if (!strcmp(cmd_info.cmd, "lcd")) {
					event_lcd(cmd_info);
					break;
				}

				else if (!strcmp(cmd_info.cmd, "passive")) {
					event_passive();
					break;

				} else if (!strcmp(cmd_info.cmd, "debug")) {
					event_debug();
					break;
				} else if (!strcmp(cmd_info.cmd, "bye")||!strcmp(cmd_info.cmd,"quit"))
				{
					if(connect_bit==0)
					{
						exit(0);
					}
				}

				else if (connect_bit == 0) {
					int not;
					not = not_connect(cmd_info);

					if (not == 1) {
						printf("Not connect\n");
						break;
					} else if (not == 0) {
						printf("?Ambiguous command\n");
						break;
					}
				}

				if (connect_bit == 1) {

					if (!strcmp(cmd_info.cmd, "user")) {
						event_user(cmd_info);
						state = I_REPLY;
						break;
					}

					else if (!strcmp(cmd_info.cmd, "ls")
							|| !strcmp(cmd_info.cmd, "dir")
							|| !strcmp(cmd_info.cmd, "nlist")) {
						if (passive_bit == 0) {
							make_PORT_arg();
						}

						int s = event_ls_dir_nlist(cmd_info); // go state S_REPLY1
						state = s;
						break;

					} else if (!strcmp(cmd_info.cmd, "get")
							|| !strcmp(cmd_info.cmd, "put")) {
						if (passive_bit == 0)
							make_PORT_arg();
						cmd_info = event_get_put(cmd_info);
						//state = S_REPLY2;
						break;
					} else if (!strcmp(cmd_info.cmd, "pwd")) {
						send_request(cmd_info.sockfd, "PWD", "");

						state = S_REPLY;
						break;

					}

					else if (!strcmp(cmd_info.cmd, "cd")) {
						event_cd(cmd_info);
						break;
					}

					else if (!strcmp(cmd_info.cmd, "ascii")) {
						send_request(cmd_info.sockfd, "TYPE A", "");
						state = S_REPLY;
						type_bit = 1;
						break;
					}

					else if (!strcmp(cmd_info.cmd, "binary")) {
						send_request(cmd_info.sockfd, "TYPE I", "");
						state = S_REPLY;
						type_bit = 0;
						break;
					}

					else if (!strcmp(cmd_info.cmd, "system")) {
						send_request(cmd_info.sockfd, "SYST", "");
						state = S_REPLY;
						break;
					}

					else if (!strcmp(cmd_info.cmd, "close")
							|| !strcmp(cmd_info.cmd, "bye")
							|| !strcmp(cmd_info.cmd, "quit")) {
						send_request(cmd_info.sockfd, "QUIT", "");
						if (!strcmp(cmd_info.cmd, "close")) {
							connect_bit = 0;
							log_bit = 0;
						}

						state = S_REPLY3;
						break;

					} else if (!strcmp(cmd_info.cmd, "type")) {
						event_type();
						break;
					} else {
						printf("?Ambiguous command\n");
						break;
					}

				}
			}
		}

		if (state == S_REPLY) {
			state_S_REPLY(cmd_info);
			state = U_CMD;

		}

		if (state == S_REPLY1) {
			state = state_S_REPLY1(cmd_info); //go S_REPLY2 or U_CMD
		}

		if (state == S_REPLY2) {
			if (passive_bit == 0)
				listenfd = Tcp_listen(my_addr, active_data_port, &addrlen);
			cmd_info = state_S_REPLY2(cmd_info);

		}

		if (state == S_REPLY3) {
			state_S_REPLY3(cmd_info);
		}

		if (state == P_REPLY) {
			state_P_REPLY(cmd_info);
		}

		if (state == DATA) {
			state_DATA(cmd_info);
		}

		if (state == I_REPLY) {
			state_I_REPLY(cmd_info);
			state = S_REPLY;
		}

	}

}

